include constants.doh

// See the README for instructions on how to access this data
import delimited "exposure_posts_regressions.csv", clear

// Make the age variables
gen age_bracket_18_34 = age_bracket == "18-24" | age_bracket == "25-34"
gen age_bracket_35_54 = age_bracket == "35-44" | age_bracket == "45-54"
gen age_bracket_55 = age_bracket == "55-64" | age_bracket == "65+"

// Make clean buckets
encode age_bracket, gen(age_bracket_clean)

// Make log RHS
gen log_fwc_jh = log(frnd_weighted_cases_jh_early)

// Make the zcta/county terciles
gen top_tercile_zcta_inc = zcta_median_hh_inc_tercile == 1
gen mid_tercile_zcta_inc = zcta_median_hh_inc_tercile == 2
gen bot_tercile_zcta_inc = zcta_median_hh_inc_tercile == 3

gen top_tercile_case100k = county_cases_per_100k_jh_tercile == 1
gen mid_tercile_case100k = county_cases_per_100k_jh_tercile == 2
gen bot_tercile_case100k = county_cases_per_100k_jh_tercile == 3

// Make friend weighted measures
egen fw_median_hh_inc_grp = cut(frnd_weighted_median_hh_inc), group(100)
egen fw_pop_density_grp = cut(frnd_weighted_pop_density), group(100)
egen fw_frac_urban_grp = cut(frnd_weighted_frac_urban), group(100)

// Make interaction group
fegen interaction_group = group(home_zcta is_female has_college age_bracket_clean has_iphone has_tablet)

// Make the new LHS variables
gen pct_posts_corona = (corona_posts_simple/(num_reshares+num_statuses)) * 100
gen pct_signed_anti_lock = (anti_lockdown_statuses/(pro_lockdown_statuses + anti_lockdown_statuses)) * 100
gen num_non_corona_posts = num_reshares + num_statuses - corona_posts_simple

// Destring certain LHS vars
rename avg_weekly_sentiment_corona_p av_wk_sent_cor
rename diff_avg_weekly_sentiment_noncor diff_av_wk_sent_noncor

// Scale up by 100
replace av_wk_sent_cor = av_wk_sent_cor * 100
replace diff_av_wk_sent_noncor = diff_av_wk_sent_noncor * 100

// Make all the necessary interaction vars
gen log_fwcXage_18_34 = log_fwc_jh*age_bracket_18_34
gen log_fwcXage_35_54 = log_fwc_jh*age_bracket_35_54
gen log_fwcXage_55 = log_fwc_jh*age_bracket_55

gen log_fwcXfemale = log_fwc_jh*(is_female==1)
gen log_fwcXmale = log_fwc_jh*(is_female==0)

gen log_fwcXcollege = log_fwc_jh*(has_college==1)
gen log_fwcXno_college = log_fwc_jh*(has_college==0)

gen log_fwcXbot_inc = log_fwc_jh*bot_tercile_zcta_inc
gen log_fwcXmid_inc = log_fwc_jh*mid_tercile_zcta_inc
gen log_fwcXtop_inc = log_fwc_jh*top_tercile_zcta_inc

gen log_fwcXbot_case100k = log_fwc_jh*bot_tercile_case100k
gen log_fwcXmid_case100k = log_fwc_jh*mid_tercile_case100k
gen log_fwcXtop_case100k = log_fwc_jh*top_tercile_case100k

gen log_fwc_jh_1_25 = log(friends_1_25_weighted_cases_jh_e)
gen log_fwc_jh_26_50 = log(friends_26_50_weighted_cases_jh_)
gen log_fwc_jh_51_75 = log(friends_51_75_weighted_cases_jh_)
gen log_fwc_jh_76_100 = log(friends_76_100_weighted_cases_jh)

///// Binscatters (Figure A11) /////

//// Percent Posts Corona ////
preserve
    // Our raw mata commands will not be as smart about missing observations as reghdfe
    drop if log_fwc_jh == .

    mata: HDFE = fixed_effects("interaction_group fw_median_hh_inc_grp fw_pop_density_grp fw_frac_urban_grp")
    mata: data = HDFE.partial_out("pct_posts_corona log_fwc_jh")
    mata: st_store(HDFE.sample, st_addvar("double", tokens("R_pct_posts_corona R_log_fwc_jh")), data)

    // Add back the means
    egen mean_pct_posts_corona = mean(pct_posts_corona)
    egen mean_log_fwc_jh = mean(log_fwc_jh)

    gen Rmean_pct_posts_corona = R_pct_posts_corona + mean_pct_posts_corona
    gen Rmean_log_fwc_jh = R_log_fwc_jh + mean_log_fwc_jh

    binscatter Rmean_*, ytitle(Share Posts about Corona (%)) xtitle(log(Friend Weighted Cases)) reportreg n(50) line(qfit) ytitle(, size(large)) ylabel(, labsize(large)) xtitle(, size(large)) xlabel(, labsize(large))

    graph export "output/figures/binscatters/pct_posts_corona_logfwc.pdf"
restore

//// Share anti-lockdown ////
preserve
    // Our raw mata commands will not be as smart about missing observations as reghdfe
    drop if log_fwc_jh == . | pct_signed_anti_lock == .

    mata: HDFE = fixed_effects("interaction_group fw_median_hh_inc_grp fw_pop_density_grp fw_frac_urban_grp")
    mata: data = HDFE.partial_out("pct_signed_anti_lock log_fwc_jh")
    mata: st_store(HDFE.sample, st_addvar("double", tokens("R_pct_signed_anti_lock R_log_fwc_jh")), data)

    // Add back the means
    egen mean_pct_signed_anti_lock = mean(pct_signed_anti_lock)
    egen mean_log_fwc_jh = mean(log_fwc_jh)

    gen Rmean_pct_signed_anti_lock = R_pct_signed_anti_lock + mean_pct_signed_anti_lock
    gen Rmean_log_fwc_jh = R_log_fwc_jh + mean_log_fwc_jh

    binscatter Rmean_*, ytitle(Share 'Signed Posts' Anti-Distancing (%)) xtitle(log(Friend Weighted Cases)) reportreg n(50) line(qfit) ytitle(, size(large)) ylabel(, labsize(large)) xtitle(, size(large)) xlabel(, labsize(large))

    graph export "output/figures/binscatters/pct_signed_anti_lock_logfwc.pdf"
restore

/////// First two columns of Table 4 //////

eststo clear

eststo panel1: reghdfe pct_posts_corona log_fwc_jh, absorb(interaction_group fw_median_hh_inc_grp fw_pop_density_grp fw_frac_urban_grp) vce(cluster i.home_zcta) tol(1e-5)
estadd ysumm

eststo panel2: reghdfe pct_signed_anti_lock log_fwc_jh, absorb(interaction_group fw_median_hh_inc_grp fw_pop_density_grp fw_frac_urban_grp) vce(cluster i.home_zcta) tol(1e-5)
estadd ysumm

xml_tab panel*, save("output/tables/frnd_exp_posts_baseline") stats(N r2 ymean) replace below


////// Pieces of Table A17 //////

eststo clear

eststo panel1: reghdfe pct_posts_corona log_fwc_jh age_bracket_35_54 age_bracket_55 is_female has_college has_iphone has_tablet mid_tercile_zcta_inc top_tercile_zcta_inc mid_tercile_case100k top_tercile_case100k, absorb(fw_median_hh_inc_grp fw_pop_density_grp fw_frac_urban_grp) vce(cluster i.home_zcta) tol(1e-5)
estadd ysumm

eststo panel2: reghdfe pct_signed_anti_lock log_fwc_jh age_bracket_35_54 age_bracket_55 is_female has_college has_iphone has_tablet mid_tercile_zcta_inc top_tercile_zcta_inc mid_tercile_case100k top_tercile_case100k, absorb(fw_median_hh_inc_grp fw_pop_density_grp fw_frac_urban_grp) vce(cluster i.home_zcta) tol(1e-5)
estadd ysumm

eststo panel3: reghdfe diff_av_wk_sent_noncor log_fwc_jh age_bracket_35_54 age_bracket_55 is_female has_college has_iphone has_tablet mid_tercile_zcta_inc top_tercile_zcta_inc mid_tercile_case100k top_tercile_case100k, absorb(fw_median_hh_inc_grp fw_pop_density_grp fw_frac_urban_grp) vce(cluster i.home_zcta) tol(1e-5)
estadd ysumm

eststo panel4: reghdfe diff_av_wk_sent_noncor log_fwc_jh, absorb(interaction_group fw_median_hh_inc_grp fw_pop_density_grp fw_frac_urban_grp) vce(cluster i.home_zcta) tol(1e-5)
estadd ysumm

xml_tab panel*, save("output/tables/frnd_exp_posts_robustness") stats(N r2 ymean) replace below


////// Pieces of Table A20 and Table A21 //////

eststo clear

eststo panel1: reghdfe pct_posts_corona log_fwcXage_18_34 log_fwcXage_35_54 log_fwcXage_55, absorb(interaction_group fw_median_hh_inc_grp fw_pop_density_grp fw_frac_urban_grp) vce(cluster i.home_zcta) tol(1e-5)
estadd ysumm

eststo panel2: reghdfe pct_posts_corona log_fwcXfemale log_fwcXmale, absorb(interaction_group fw_median_hh_inc_grp fw_pop_density_grp fw_frac_urban_grp) vce(cluster i.home_zcta) tol(1e-5)
estadd ysumm

eststo panel3: reghdfe pct_posts_corona log_fwcXcollege log_fwcXno_college, absorb(interaction_group fw_median_hh_inc_grp fw_pop_density_grp fw_frac_urban_grp) vce(cluster i.home_zcta) tol(1e-5)
estadd ysumm

eststo panel4: reghdfe pct_posts_corona log_fwcXbot_inc log_fwcXmid_inc log_fwcXtop_inc, absorb(interaction_group fw_median_hh_inc_grp fw_pop_density_grp fw_frac_urban_grp) vce(cluster i.home_zcta) tol(1e-5)
estadd ysumm

eststo panel5: reghdfe pct_posts_corona log_fwcXbot_case100k log_fwcXmid_case100k log_fwcXtop_case100k, absorb(interaction_group fw_median_hh_inc_grp fw_pop_density_grp fw_frac_urban_grp) vce(cluster i.home_zcta) tol(1e-5)
estadd ysumm

eststo panel6: reghdfe pct_posts_corona log_fwc_jh_1_25 log_fwc_jh_26_50 log_fwc_jh_51_75 log_fwc_jh_76_100, absorb(interaction_group fw_median_hh_inc_grp fw_pop_density_grp fw_frac_urban_grp) vce(cluster i.home_zcta) tol(1e-5)
estadd ysumm
test log_fwc_jh_1_25 = log_fwc_jh_76_100
estadd scalar F_top_bot = r(F)
estadd scalar p_top_bot = r(p)

xml_tab panel*, save("output/tables/frnd_exp_posts_heteros_pct_posts_corona") stats(N r2 ymean F_top_bot p_top_bot) replace below

eststo clear

eststo panel1: reghdfe pct_signed_anti_lock log_fwcXage_18_34 log_fwcXage_35_54 log_fwcXage_55, absorb(interaction_group fw_median_hh_inc_grp fw_pop_density_grp fw_frac_urban_grp) vce(cluster i.home_zcta) tol(1e-5)
estadd ysumm

eststo panel2: reghdfe pct_signed_anti_lock log_fwcXfemale log_fwcXmale, absorb(interaction_group fw_median_hh_inc_grp fw_pop_density_grp fw_frac_urban_grp) vce(cluster i.home_zcta) tol(1e-5)
estadd ysumm

eststo panel3: reghdfe pct_signed_anti_lock log_fwcXcollege log_fwcXno_college, absorb(interaction_group fw_median_hh_inc_grp fw_pop_density_grp fw_frac_urban_grp) vce(cluster i.home_zcta) tol(1e-5)
estadd ysumm

eststo panel4: reghdfe pct_signed_anti_lock log_fwcXbot_inc log_fwcXmid_inc log_fwcXtop_inc, absorb(interaction_group fw_median_hh_inc_grp fw_pop_density_grp fw_frac_urban_grp) vce(cluster i.home_zcta) tol(1e-5)
estadd ysumm

eststo panel5: reghdfe pct_signed_anti_lock log_fwcXbot_case100k log_fwcXmid_case100k log_fwcXtop_case100k, absorb(interaction_group fw_median_hh_inc_grp fw_pop_density_grp fw_frac_urban_grp) vce(cluster i.home_zcta) tol(1e-5)
estadd ysumm

eststo panel6: reghdfe pct_signed_anti_lock log_fwc_jh_1_25 log_fwc_jh_26_50 log_fwc_jh_51_75 log_fwc_jh_76_100, absorb(interaction_group fw_median_hh_inc_grp fw_pop_density_grp fw_frac_urban_grp) vce(cluster i.home_zcta) tol(1e-5)
estadd ysumm
test log_fwc_jh_1_25 = log_fwc_jh_76_100
estadd scalar F_top_bot = r(F)
estadd scalar p_top_bot = r(p)

xml_tab panel*, save("output/tables/frnd_exp_posts_heteros_pct_signed_anti_lock") stats(N r2 ymean F_top_bot p_top_bot) replace below

eststo clear

eststo panel1: reghdfe diff_av_wk_sent_noncor log_fwcXage_18_34 log_fwcXage_35_54 log_fwcXage_55, absorb(interaction_group fw_median_hh_inc_grp fw_pop_density_grp fw_frac_urban_grp) vce(cluster i.home_zcta) tol(1e-5)
estadd ysumm

eststo panel2: reghdfe diff_av_wk_sent_noncor log_fwcXfemale log_fwcXmale, absorb(interaction_group fw_median_hh_inc_grp fw_pop_density_grp fw_frac_urban_grp) vce(cluster i.home_zcta) tol(1e-5)
estadd ysumm

eststo panel3: reghdfe diff_av_wk_sent_noncor log_fwcXcollege log_fwcXno_college, absorb(interaction_group fw_median_hh_inc_grp fw_pop_density_grp fw_frac_urban_grp) vce(cluster i.home_zcta) tol(1e-5)
estadd ysumm

eststo panel4: reghdfe diff_av_wk_sent_noncor log_fwcXbot_inc log_fwcXmid_inc log_fwcXtop_inc, absorb(interaction_group fw_median_hh_inc_grp fw_pop_density_grp fw_frac_urban_grp) vce(cluster i.home_zcta) tol(1e-5)
estadd ysumm

eststo panel5: reghdfe diff_av_wk_sent_noncor log_fwcXbot_case100k log_fwcXmid_case100k log_fwcXtop_case100k, absorb(interaction_group fw_median_hh_inc_grp fw_pop_density_grp fw_frac_urban_grp) vce(cluster i.home_zcta) tol(1e-5)
estadd ysumm

eststo panel6: reghdfe diff_av_wk_sent_noncor log_fwc_jh_1_25 log_fwc_jh_26_50 log_fwc_jh_51_75 log_fwc_jh_76_100, absorb(interaction_group fw_median_hh_inc_grp fw_pop_density_grp fw_frac_urban_grp) vce(cluster i.home_zcta) tol(1e-5)
estadd ysumm
test log_fwc_jh_1_25 = log_fwc_jh_76_100
estadd scalar F_top_bot = r(F)
estadd scalar p_top_bot = r(p)

xml_tab panel*, save("output/tables/frnd_exp_sentiment_heteros_diff_all_posts") stats(N r2 ymean F_top_bot p_top_bot) replace below

eststo clear
